package com.cqupt.sendMes.utils;

import cn.hutool.core.util.ObjectUtil;
import com.cqupt.sendMes.enums.HttpStatusEnum;
import com.cqupt.sendMes.exception.BusinessException;
import com.cqupt.sendMes.vo.UserInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
public class UserHelper {

    public static UserInfoVO getCurrentUser() {

        Map<String, Object> claims = ThreadLocalUtil.get(); //获取当前线程中的用户信息
        if (ObjectUtil.isNull(claims)) {
            log.warn("操作失败，读取当前线程用户失败");
            throw new BusinessException(HttpStatusEnum.OBJECT_NOT_EXIST.getCode(), HttpStatusEnum.OBJECT_NOT_EXIST.getMessage());
        }
        UserInfoVO userInfoVO = UserInfoVO.builder()
                .id((Integer) claims.get("id"))
                .username((String) claims.get("username"))
                .roleId((Integer) claims.get("roleId"))
                .roleName((String) claims.get("roleName"))
                .userUid((String) claims.get("userUid"))
                .status((Integer) claims.get("status"))
                .build();
        if (ObjectUtil.isNull(userInfoVO)) {
            log.warn("操作失败，读取用户失败");
            throw new BusinessException(HttpStatusEnum.OBJECT_NOT_EXIST.getCode(), HttpStatusEnum.OBJECT_NOT_EXIST.getMessage());
        }
        return userInfoVO;

    }
}
